#include<iostream>
using namespace std;

//根据函数调用，推导出模板参数的类型，实例化出函数


//一个参数
template <class T>

T  func1(const T& x,const T& y)
{
	return x + y;
}

//多个参数
template <class T1,class T2>
void  func2(const T1& x, const T2& y)
{
	cout << x << " " << y << endl;
}

//stack 类
template <class T>
class stack
{
public:
	stack(int capacity = 4)
	{
		_array = new T[capacity];
		_size = 0;
		_capacity = capacity;
	}
	void push(const T& x)
	{
		_array[_size] = x;
		_size++;
	}
	void print()
	{
		for (int i = 0; i < _size; i++)
			cout << _array[i++] << " " << endl;
	}
	~stack()
	{
		delete _array;
		_size = 0;
		_capacity = 0;
	}
private:
	T* _array;
	int _size;
	int _capacity;
};

int main()
{
	//隐式实例化-->根据传递的实参自动推导
	int a = 10, b = 20;
	double c = 10.1, d = 20.1;
	cout << func1(a, b) << endl;   //int
	cout << func1(c, d) << endl;   //double
	cout << func1(a, (int)c) << endl;   //int
	cout << func1((double)b, d) << endl;   //double

	//显示实例化-->隐式类型转换
	cout << func1<int>(a, c) << endl;
	cout << func1<double>(b, d) << endl;

	func2(a, c);  //int double

	//普通类 类名和类型是一样的
	//模板类 类名stack 类型stack<T> 在外面定义类内成员函数时需要加上类型
	stack<int> st1(8);
	st1.push(4);
	st1.print();

	stack<double> st2(8);
	st2.push(4.4);
	st2.print();
	

}

